package com.nyist.diseasepreventionserver.service.impl;

import com.nyist.diseasepreventionserver.common.ResponseCode;
import com.nyist.diseasepreventionserver.common.UserPowerCode;
import com.nyist.diseasepreventionserver.converter.User2UserDTOConverter;
import com.nyist.diseasepreventionserver.dto.UserDTO;
import com.nyist.diseasepreventionserver.entity.HealthDaily;
import com.nyist.diseasepreventionserver.entity.Role;
import com.nyist.diseasepreventionserver.entity.User;
import com.nyist.diseasepreventionserver.repository.UserDAO;
import com.nyist.diseasepreventionserver.service.RoleService;
import com.nyist.diseasepreventionserver.service.UserService;
import com.nyist.diseasepreventionserver.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserDAO  userDAO;
    @Override
    public UserDTO register(UserDTO userDTO) {
        //注册时间
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(simpleDateFormat.format(date));
        //设置权限
        List<Role> roleList = new ArrayList<>();
        Role role = roleService.findOne(UserPowerCode.power_user.getCode());
        roleList.add(role);
        user.setRoles(roleList);
        //初始化用户健康日报
        HealthDaily healthDaily = new HealthDaily();
        user.setHealthDaily(healthDaily);

        //写入数据库
        userDAO.save(user);
        return userDTO;
    }

    @Override
    public UserDTO login(Integer userSid, String password) {
        User user = userDAO.findByUserSidAndUserPassword(userSid, password);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return userDTO;

    }

    @Override
    public UserDTO findOne(Integer userSid) {
        User user = userDAO.findByUserSid(userSid);
        UserDTO userDTO = new UserDTO();
        //判断学号是否存在
        if (user != null){ //存在的情况，把user转换为userDTO 返回数据给controller处理
            BeanUtils.copyProperties(user,userDTO);
            return userDTO;
        }else { //不存在的情况
            return userDTO;
        }
    }

    @Override
    public Page<UserDTO> findAll(Pageable pageable) {
        Page<User> userPage = userDAO.findAll(pageable);
        List<UserDTO> userDTOList= User2UserDTOConverter.convert(userPage.getContent());
        PageImpl<UserDTO> userDTOPage = new PageImpl<>(userDTOList, pageable, userPage.getTotalElements());
        return userDTOPage;
    }

}
